Sharable utilities (coordinates conversion)
Estimated duration : 45 minutes
Presentation :
Let's consider a game where the map displays my location as well as the location of other players and all sorts of ennemies moving around the city.- When writing code for sprites, we will use canvas or pixel coordinates,
- Our geo-location as well as the geo-location of other players and ennemies will be known with coordinates in degrees of longitude and latitude,
- Maps will be spatially defined by their center in longitude and latitude, and pixel size (width and height) in meters whch depends on map scale,
- Distances between 2 points with coordinates in degrees will be measured in meters.
We need to switch from each coordinate system to the other ones, and thta's what we will do here :
- Convert location of other players (longitude and latitude degrees from their GPS) to our screen canvas(with pixel coordinates),
- Compute distance in meters or kilometers between 2 poinst known by their Latitude and longitude in meters,
- ...
Reminder on geographic coordinates (latitude and longitude) :
May be we can remind what are the Longitude, Latitude and altitude of a point :
- Longitude λ is an angle that specifies the east-west position of a point on the Earth's surface. 0° at the Prime Meridian in Greenwich. It ranges from −180° westward to +180° eastward.
- Latitude φ is an angle that specifies the north–south position of a point on the Earth's surface. It ranges from -90° at the south pole, 0° at the equator to +90° at the north pole.
- Altitude is the height in meters above mean sea level (MSL), not to be confused with height above ground (AGL). Terrain elevation is the altitude (MSL) of the ground itself
What you will do :
- Review/understand what coordinate systems are at stake,
- Review the list of conversion and computation routines needed,
- (optional)Look at conversion rules and algorithms,
- Practice their use.
What you will learn :
- List of coordinates systems,
- List of useful conversion routines (and underlying assumptions),
- (optional)conversion methods and algorithms,
- use of conversion procedures.
Input Resources :
Conversion libraries are available at :Lesson content :
What do we need :
The functions we need (and that are made available) are :function (procedure) | function name and parameters |
---|---|
length in meters of a degree of latitude | degreeOfLatitudeMeters() |
length in meters of a degree of longitude | degreeOfLongitudeMeters(latitude) |
distance in meters between 2 points from their longitude and latitude (on the spherical earth) : | distanceMeters(lat1, long1, lat2,long2) |
width in degrees of a pixel | pixelWidthInDegrees(zoom) |
height in degrees of a pixel | pixelHeightInDegrees(zoom, latitude) |
size of a pixel in meters | pixelsizeInMeters(zoom, latitude) |
longitude of pixel at x | longitudeFromX(x, canvasWidth,centerlat, centerLong,zoom) |
latitude of pixel at y | |
X pixel coordinate from latitude longitude | FromLatLong (longitude, canvasWidth, centerLatitude, centerLongitude, zoom) |
Y pixel coordinate from latitude : | yFromLat (latitude, canvasHeight, centerLatitude, zoom) |
Don’t be afraid : :
You will NOT have to code these functions, and you do not need to understand the details of what's inside.But You need to have a clear view of what these functions do and when to use them (we will see examples).
You should also be able to check that they work well on practical cases. (We have done error checking, but Errors are still possible. When you reuse : trust but check).
Length in meters of one degree of latitude and of longitude

A degree of latitude is then equal to 10 000 000 m / 90° ~ 111 111 m. However, we will here assume a spherical earth with an average radius rounded to 6 371 km Leading to 1° = 111 195 m. The reason is that we want to use a single constant for all formulas : the earth (or planet) radius.
A degree of longitude changes with latitude :
length of a degree of longitude = length of a degree of latitude * cosine (latitude)
with the corresponding App inventor code here
The length of a degree of latitude in meters is then given by :

The length of a degree of longitude in meters is then given by :

Distance between 2 points known by their longitude λ and latitude φ

When distances are limited you may use the Pythagorean theorem :

leading to

Which has a precision of 1% within 4000 km. But we will use the more precise Vincenty formula (see Wikipedia) of the angular distance :

Which we multiply by the radius of the earth, to get the distance in meters.
The distance in meters between 2 points on the earth known by their longitudes and latitudes lat1, long1, lat2,long2 is then:

Pixel Width and height in degrees of longitude and latitude
The operation that converts latitude and longitude (λ,φ) to pixel coordinates (x,y) is the "web Mercator" projection (see Wikipedia) with the following formulas :
From which follows in radians :

or for us in degrees :

Translated into the following blocks :


Pixel size in meters
- Pixel height in meters = pixel height in radians multiplied by radius of a meridian [i.e. Rearth]
- Pixel width in meters = pixel width in radians multiplied by radius of the parallel at given latitude [i.e. Rearth * cos(φ) ]

longitude and latitude from pixel-canvas coordinates

Canvas pixel coordinates are illustred by the figure on the right with origin is top left.
- in green : canvas pixels with center position and x,y values,
- in red : center latitude and longitude, pixel height and width,
- in blue : physical screen coordinates (canvas units).
When map is oriented north, x increases with longitude and y decreases with latitude (opposite direction).
For pixel x,y returned by App Inventor blocks it follows :

longitude from pixel x coordinate and canvas center lat

Latitude from pixel y coordinate

pixel-canvas coordinates from longitude and latitude Inversion of the previous functions is straightforward,
a point with "latitude, longitude" coordinates should be plotted at x, y given by :

If x or y are less than 0 or greater than canvas width or height, point is out of current map view.
X pixel coordinate from latitude longitude and center

Y pixel coordinate from latitude and center
